package ui;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.ButtonModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

/** 
 * @author Matthias
 * @author Niels Christian Mouritsen
 */
public class AccWithdrawal extends JPanel{
	
	private final JLabel lbl_Header;
	private final JLabel lbl_Intro;
	private final JLabel lbl_CurrentBalance;
	private final JLabel lblForCustomAmount;
	private final JLabel logo_ATM;
	private final JTextField textField_CurrentBalance;
	private final JButton btn_Back;
	private final JButton btn_Withdraw;
	private final JButton btn_CustomAmount;
	private final JRadioButton rdbtn_20;
	private final JRadioButton rdbtn_40;
	private final JRadioButton rdbtn_60;
	private final JRadioButton rdbtn_80;
	private final JRadioButton rdbtn_100;
	private final JRadioButton rdbtn_200;
	private final ButtonGroup buttonGroup = new ButtonGroup();

	/**
	 * A String object, the input of amount
	 */
	private String input;
	
	/**
	 * Constructor of AccWithdrawal, setting dimensions and layout
	 */
	public AccWithdrawal() {
		
		setPreferredSize(new Dimension(600, 480));
		setLayout(null);
		
		lbl_Header = new JLabel("Account Withdrawal");
		lbl_Header.setFont(new Font("SansSerif", Font.BOLD, 26));
		lbl_Header.setBounds(168, 25, 252, 34);
		add(lbl_Header);
		
		lbl_Intro = new JLabel("Chose an ammount:");
		lbl_Intro.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_Intro.setBounds(51, 97, 155, 21);
		add(lbl_Intro);
		
		btn_Back = new JButton("Back");
		btn_Back.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Back.setBounds(51, 367, 130, 50);
		add(btn_Back);
		
		btn_Withdraw = new JButton("Withdraw");
		btn_Withdraw.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_Withdraw.setBounds(370, 367, 130, 50);
		add(btn_Withdraw);
		
		rdbtn_20 = new JRadioButton("20");
		rdbtn_20.setActionCommand("20");
		rdbtn_20.setFont(new Font("SansSerif", Font.PLAIN, 16));
		buttonGroup.add(rdbtn_20);
		rdbtn_20.setBounds(51, 122, 66, 29);
		add(rdbtn_20);
		
		rdbtn_40 = new JRadioButton("40");
		rdbtn_40.setActionCommand("40");
		rdbtn_40.setFont(new Font("SansSerif", Font.PLAIN, 16));
		buttonGroup.add(rdbtn_40);
		rdbtn_40.setBounds(168, 122, 70, 29);
		add(rdbtn_40);
		
		rdbtn_60 = new JRadioButton("60");
		rdbtn_60.setActionCommand("60");
		rdbtn_60.setFont(new Font("SansSerif", Font.PLAIN, 16));
		buttonGroup.add(rdbtn_60);
		rdbtn_60.setBounds(51, 158, 66, 29);
		add(rdbtn_60);
		
		rdbtn_80 = new JRadioButton("80");
		rdbtn_80.setActionCommand("80");
		rdbtn_80.setFont(new Font("SansSerif", Font.PLAIN, 16));
		buttonGroup.add(rdbtn_80);
		rdbtn_80.setBounds(168, 164, 70, 23);
		add(rdbtn_80);
		
		rdbtn_100 = new JRadioButton("100");
		rdbtn_100.setActionCommand("100");
		rdbtn_100.setFont(new Font("SansSerif", Font.PLAIN, 16));
		buttonGroup.add(rdbtn_100);
		rdbtn_100.setBounds(51, 198, 66, 29);
		add(rdbtn_100);
		
		rdbtn_200 = new JRadioButton("200");
		rdbtn_200.setActionCommand("200");
		rdbtn_200.setFont(new Font("SansSerif", Font.PLAIN, 16));
		buttonGroup.add(rdbtn_200);
		rdbtn_200.setBounds(168, 198, 70, 29);
		add(rdbtn_200);
		
		btn_CustomAmount = new JButton("Custom Amount");
		btn_CustomAmount.setFont(new Font("SansSerif", Font.PLAIN, 16));
		btn_CustomAmount.setBounds(350, 234, 150, 50);
		add(btn_CustomAmount);
		
		textField_CurrentBalance = new JTextField();
		textField_CurrentBalance.setFont(new Font("SansSerif", Font.BOLD | Font.ITALIC, 16));
		textField_CurrentBalance.setHorizontalAlignment(SwingConstants.CENTER);
		textField_CurrentBalance.setEditable(false);
		textField_CurrentBalance.setBounds(340, 122, 160, 30);
		add(textField_CurrentBalance);
		textField_CurrentBalance.setColumns(10);
		
		lbl_CurrentBalance = new JLabel("Current Balance:");
		lbl_CurrentBalance.setFont(new Font("SansSerif", Font.BOLD, 16));
		lbl_CurrentBalance.setBounds(312, 97, 130, 21);
		add(lbl_CurrentBalance);
		
		lblForCustomAmount = new JLabel("For Custom Amount press:");
		lblForCustomAmount.setFont(new Font("SansSerif", Font.BOLD, 16));
		lblForCustomAmount.setBounds(293, 202, 207, 21);
		add(lblForCustomAmount);
		
		logo_ATM = new JLabel("");
		logo_ATM.setIcon(new ImageIcon(AccWithdrawal.class.getResource("/pic/LOGO Mini.png")));
		logo_ATM.setBounds(15, 15, 50, 50);
		add(logo_ATM);
	}
	
	/** Adds action listeners
	 * @param e
	 */
	public final void addActionListener (final ActionListener e){
		btn_Back.addActionListener(e);
		btn_Withdraw.addActionListener(e);
		btn_CustomAmount.addActionListener(e);
	}
	
	/** Enables radio buttons to be set to false
	 * 	Can Clear the selection of buttons
	 */
	public final void setRdbtnsFalse() {
		rdbtn_20.setEnabled(false);
		rdbtn_40.setEnabled(false);
		rdbtn_60.setEnabled(false);
		rdbtn_80.setEnabled(false);
		rdbtn_100.setEnabled(false);
		rdbtn_200.setEnabled(false);
		buttonGroup.clearSelection();
	}
	
	/** Enables radio button to be set to true
	 * 	Can clear the selection of buttons
	 */
	public final void setRdbtnsTrue() {
		rdbtn_20.setEnabled(true);
		rdbtn_40.setEnabled(true);
		rdbtn_60.setEnabled(true);
		rdbtn_80.setEnabled(true);
		rdbtn_100.setEnabled(true);
		rdbtn_200.setEnabled(true);
		buttonGroup.clearSelection();
	}
	
	/**
	 * @return the back button
	 */
	public final JButton getBtnBack() {
		input = "";
		setRdbtnsTrue();
		return btn_Back;
	}
	
	/**
	 * @return the withdrawal button
	 */
	public final JButton getBtnWithdrawal() {
		return btn_Withdraw;
	}
	
	/**
	 * @return custom amount button
	 */
	public final JButton getBtnCustomAmount() {
		return btn_CustomAmount;
	}
	
	/** Displays the current total balance of the chosen account
	 * @param totalBalance does represent the current total balance
	 */
	public final void showField(final double totalBalance) {
		textField_CurrentBalance.setText("" + totalBalance);
	}
	
	/**
	 * Input is set to zero at startup
	 */
	public final void setInput() {
		input = null;
	}
	
	/**
	 * If there is NO input in custom amount, radio buttons are set to true
	 * If there is AN input in custom amount, radio buttons are set to false
	 */
	public final void showDialog() {
		input = 
			JOptionPane.showInputDialog(
				null, "", "Enter custom amount", JOptionPane.OK_CANCEL_OPTION);
		if (input == null) {
			// if button cancel is clicked it goes back to the screen
			setRdbtnsTrue();
		}
		else {
			// enables any radio button to be clicked, 
			// in order to avoid misunderstandings
			setRdbtnsFalse();
			String text = "Withdraw input: " + input + "\nPlease confirm "
					 + "the withdrawal by clicking Withdraw";
					new MessagePopUp().setSuccess(text);	
		}
	}
	
	/**
	 * @return input in the custom amount box
	 */
	public final double getWithdrawInputOfDialog() {
		if (input == null) {
			return 0;
		}
		else {
			return Double.parseDouble(input);
		}
	}
			
	/** 
	 * 	Only works if while selected, no input is given in custom amount
	 * @return selected radio button if any are selected, 
	 * plus the amount that is given to it
	 */
	public final double getWithdrawRdbtnAmount() {
		setInput();
		ButtonModel model = buttonGroup.getSelection();
		if (model == null) { // no radiobutton selected
			return 0;
		} else {
			buttonGroup.clearSelection();
			return Integer.parseInt(model.getActionCommand());
		}
	}
	
} // end class AccWithdrawal